import { getStorage } from "../utils/localStorage"
// export const baseUrl = `http://zjhlocaltown.go.cnitcs.net`; //测试地址1
// export const baseUrl = `http://swklocaltown.go.cnitcs.net`; //测试地址2
// export const baseUrl = `https://www.nbomb.cloud:8089`;  //线上地址
export const baseUrl = `http://192.168.6.243:8082`;  //本地地址


export const request = (method, url, data, loadingFlag) => {
  if (data) {
    let userinfo = getStorage('user')
    if(!data.userId&&userinfo.id)data.userId = userinfo.id;
    if(!data.wxOpenid&&userinfo.wxOpenid)data.wxOpenid = userinfo.wxOpenid;
  }
  return new Promise((resolve, reject) => {
    //调用接口是否显示loading效果tru和false
    if (loadingFlag) {
      wx.showLoading({
        title: "数据加载中...",
        mask: true,
      });
    };
    wx.request({
      url: baseUrl + url,
      method: method,
      header: {
        "content-type": "application/json",
        'Cookie': "token=" + getStorage('token'),
      },
      data: data || {},
      timeout: 100000,
      success: (res) => {
        if (res.data.code === 200) {
          resolve(res);
        } else if (res.data.code === 401) {
          wx.showModal({
            content: '身份信息已过期请重新登录',
            confirmText: '确定',
            cancelText: '取消',
            success: () => {
              wx.redirectTo({
                url: '/pages/bootPage/index',
              })
            }
          });
        } else if (res.data.code === 500) {
          wx.showToast({
            icon: 'none',
            title: res.data.msg,
          });
          reject(res);
        };
        //关闭loadding效果
        if (loadingFlag) {
          wx.hideLoading({
            noConflict: true
          })
        };
      },
      fail: (err) => {
        setTimeout(() => {
          wx.showToast({
            icon: "none",
            title: err.errMsg,
          })
        }, 1000);
        //关闭loadding效果
        if (loadingFlag) {
          wx.hideLoading({
            noConflict: true
          })
        };
      }
    });
  })
}


//上传图片上传方法
export function chooseImageTap(fun) {
  wx.showActionSheet({
    itemList: ['从相册中选择', '拍照'],
    itemColor: "#666",
    success: function (res) {
      if (!res.cancel) {
        var type = "";
        if (res.tapIndex == 0) {
          chooseWxImage('album', fun)
        } else if (res.tapIndex == 1) {
          chooseWxImage('camera', fun)
        }
      }
    }
  })
};

//选择图片上传
function chooseWxImage(type, fun) {
  wx.chooseImage({
    sizeType: ['original', 'compressed'],
    sourceType: [type],
    count: 1,
    success: function (res) {
      // console.log(res)
      upImgs(res.tempFilePaths[0], fun) //调用上传方法
    }
  })
};

//上传图片
function upImgs(imgurl, fun) {
  wx.showLoading({
    title: '上传中',
  });
  wx.uploadFile({
    url: baseUrl + '/common/upload',
    filePath: imgurl,
    name: 'file',
    header: {
      "Content-Type": "multipart/form-data",
    },
    formData: null,
    success: function (res) {
      var data = JSON.parse(res.data)
      if (data.code == 200) {
        wx.showToast({
          icon: 'success',
          title: '图片上传成功',
        });
        if (fun) {
          fun(data)
        };
      } else {
        wx.showToast({
          icon: 'error',
          title: '图片上传失败',
        })
      }
    },
    fail: function (err) {
      wx.showToast({
        icon: 'error',
        title: '图片上传失败',
      })
    },
    complete: function () {
      wx.hideLoading();
    }
  })
};
